﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufDropDownBoard.h"
#include "GnufQtItem.h"
#include <QButtonGroup>
#include <QFrame>
#include "GnufQtScrollArea.h"
#include <QVBoxLayout>
class QPushButton;

namespace gnuf {
    class DropDownBoardAppearanceSetting;
    class QtText;
    class QtSeparator;
    class QtToolButton;
    class QtLineEditComboBox;
    class QtDropDownBoard final : public QtItemWith<QWidget, DropDownBoard, QWidget*>
    {
        Q_OBJECT
    public:
        explicit QtDropDownBoard(DropDownBoard*, QWidget* parent = nullptr);
        virtual ~QtDropDownBoard();

    public:
        bool addMenuItem(QtItem*);
        bool insertMenuItem(int idx, QtItem*);
        bool removeMenuItem(QtItem*);
        void addSeparator();
        void removeSeparator(int index);
        int menuItemCount() const;
        int groupIndexOfQtItem(QtItem*);

        void updateGroupVisibilty(int index, bool visible);

        //virtual QSize sizeHint() const;
        void refreshDisplayNumPerPage();
        void adjustSize();
        void adjustSizeForLineEditCombobox(int num = 10);
        int getSuitableHeight(int num = 10);
        bool isBuilt() const { return m_isBuilt; }

        static bool turnOnJournalUiRecorder();
        static bool turnOffJournalUiRecorder();

    public slots:
        void popup(int x, int y);
        void popupForLineEditCombobox(int x, int y);
        void closeBoard();
        void onPushButtonClicked(bool);
        void onCheckButtonClicked(bool);
    protected:
        virtual void refresh(QtItem::EWhatDirty) override;
        virtual void paintEvent(QPaintEvent *) override;
        virtual void disconnectFromItem() override;
        virtual bool eventFilter(QObject *, QEvent *) override;
    private:
        void buildContents();
        QtItem* buildItem(Item*);
        void setStyle();
        int getRealIndex(int idx) const;
        void addToButtonGroup(QPushButton*);
        void removeFromButtonGroup(QPushButton*);
        void initScrollArea(DropDownBoard*);
        QWidget* getBoardWidget();
        const QWidget* getBoardWidget() const;

    private:
        QtScrollArea* m_qtScrollArea = nullptr;
        QWidget* m_scrollAreaWidget = nullptr;
        QtText* m_caption = nullptr;
        QButtonGroup* m_buttonGroup = nullptr;
        std::vector<QtSeparator*> m_separators;
        bool m_isBuilt = false;

        static bool s_turnOnJournalUiRecorder;

        friend class DropDownBoard;
        friend class QtToolButton;
        friend class QtLineEditComboBox;
        friend class QtTagPad;
    };
}
